<?php
    class User extends CActiveRecord{
        public static function model($className = __CLASS__) {
            return parent::model ( $className );
        }

        // dat luat cho model
        public function rules(){}         

        // goi den bang can ket noi   
        public function tableName() {                  
            return 'pm_user';
        }

        // nhan cac thuong tinh 
        public function attributeLabels(){}

        public function getRowByUsername($username){
            //$id = intval($id);
            // $cache = false;
            $row = array();
            $cache = Yii::app()->cache;
            if($cache != null){
                $cacheService = new CacheService("User","getRowByUsername",$username);
                $key = $cacheService->createKey();
                $dependency = $cacheService->createDependency();
                $cache = Yii::app ()->cache->get ( $key );
                //echo $dependency;  
            }
            $cache=false;
            if($cache == false){ 
                $sql = "SELECT * FROM pm_user WHERE username='".$username."'";           
                $connect = Yii::app()->db;
                $command = $connect->createCommand($sql);
                $row = $command->queryRow();
                Yii::app ()->cache->set ( $key, $row, ConstantsUtil::TIME_CACHE_3600, new CGlobalStateCacheDependency ( $dependency ) );
            }else{
                $row = $cache;
            }
            return $row;
        }

        public function getRowByEmail($email){
            //$id = intval($id);
            // $cache = false;
            $row = array();
            $cache = Yii::app()->cache;
            if($cache != null){
                $cacheService = new CacheService("User","getRowByEmail",$email);
                $key = $cacheService->createKey();
                $dependency = $cacheService->createDependency();
                $cache = Yii::app ()->cache->get ( $key );
                //echo $dependency;  
            }
            $cache=false;
            if($cache == false){ 
                $sql = "SELECT * FROM pm_user WHERE email='".$email."'";           
                $connect = Yii::app()->db;
                $command = $connect->createCommand($sql);
                $row = $command->queryRow();
                Yii::app ()->cache->set ( $key, $row, ConstantsUtil::TIME_CACHE_3600, new CGlobalStateCacheDependency ( $dependency ) );
            }else{
                $row = $cache;
            }
            return $row;
        }

        public function getRowById($id){
            $id = intval($id);
            // $cache = false;
            $row = array();            
            $sql = "SELECT * FROM pm_user WHERE user_id=".$id;                      
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $row = $command->queryRow();                
            return $row;
        }

        public function getRowByMobile($mobile){
            //$id = intval($id);
            // $cache = false;
            $row = array();
            $cache = Yii::app()->cache;
            if($cache != null){
                $cacheService = new CacheService("User","getRowByMobile",$mobile);
                $key = $cacheService->createKey();
                $dependency = $cacheService->createDependency();
                $cache = Yii::app ()->cache->get ( $key ); 
            }
            //$cache=false;
            if($cache == false){ 
                $sql = "SELECT * FROM user WHERE mobile='".$mobile."'";           
                $connect = Yii::app()->db;
                $command = $connect->createCommand($sql);
                $row = $command->queryRow();

                Yii::app ()->cache->set ( $key, $row, ConstantsUtil::TIME_CACHE_3600, new CGlobalStateCacheDependency ( $dependency ) );
            }else{
                $row = $cache;
            }
            return $row;
        }

        public function insertRow($username,$password,$fullname,$email,$user_birthday,$user_mobile,$user_address,$filepath,$personal_id){
            $date = time();
            $last_id =0;
            $connect = Yii::app()->db;        
            $sql = "INSERT INTO pm_user(username,password,fullname,email,mobile,address,birthday,image,personal_id,create_date) VALUES(:username, :password, :fullname, :email, :mobile, :address, :birthday, :image, :personal_id, :create_date)";
            $command = $connect->createCommand ($sql);
            $command->bindValue ( ":username", $username, PDO::PARAM_STR ); 
            $command->bindValue ( ":password", $password, PDO::PARAM_STR );          
            $command->bindValue ( ":fullname", $fullname, PDO::PARAM_STR ); 
            $command->bindValue ( ":email", $email, PDO::PARAM_STR );        
            $command->bindValue ( ":mobile", $user_mobile, PDO::PARAM_STR );        
            $command->bindValue ( ":address", $user_address, PDO::PARAM_STR );        
            $command->bindValue ( ":birthday", $user_birthday, PDO::PARAM_STR );        
            $command->bindValue ( ":image", $filepath, PDO::PARAM_STR );        
            $command->bindValue ( ":personal_id", $personal_id, PDO::PARAM_STR );        
            $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );         
            $result = $command->execute();
            $last_id = Yii::app()->db->getLastInsertId();
            return $last_id;

        }

        public function updateAvatarById($id,$avatar_url){
            $date = date('Y-m-d',time());
            $row = array();                        
            $sql = "UPDATE `user` SET `avatar_url` = '".$avatar_url."',`modify_date` = '".$date."' WHERE `user`.`id` = ".$id." ";
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $row = $command->execute();            
            return $row;
        }

        public function getRowByUsernameAndPass($username,$pass){
            $connect = Yii::app()->db;
            $row = array();
            $sql = "SELECT * FROM pm_user WHERE username='".$username."' AND password ='".$pass."'";        
            $command = $connect->createCommand($sql);
            $row = $command->queryRow();
            return $row;
        }

        public function getMd5($value){
            $val = md5($value);
            return $val;
        }

        public function getAllRows(){
            $rows = array();
            $sql = "SELECT pm_user.*,pm_member.project_id FROM pm_user LEFT JOIN pm_member ON pm_user.user_id = pm_member.user_id";
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $rows = $command->queryAll();            
            return $rows;
        }

        public function getUserByArrId($arr_id){
            $row = array();
            $sql = "SELECT id, username, mobile FROM user WHERE id IN(".$arr_id.")";
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $row = $command->queryAll(); 
            return $row; 
        }

        public function checkUserByUserName($user_name){
            $row = array();
            $sql = "SELECT id FROM user WHERE username = '$user_name'";
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $row = $command->queryRow(); 
            return $row;
        }

        public function updateObject($array_input,$key_id,$key_value){
            $sql='';
            foreach($array_input as $key=>$value)
            {
                $sql.=$key."='".$value."',"; 
            }
            $sql=rtrim($sql,',');
            if($sql!='')
            {
                $sql='UPDATE pm_user SET '.$sql.' WHERE '.$key_id.'='.$key_value;
                $connect = Yii::app()->db;
                $command = $connect->createCommand($sql);
                $a=$command->execute();
                return $a;
            }
            return -1;
        }    

        public function deleteObject($array_input){
            $sql=' 1 ';
            foreach($array_input as $key=>$value)
            {
                $sql.=" AND ".$key."='".$value."'"; 
            }
            $sql=rtrim($sql,',');
            if($sql!='')
            {
                $sql='delete from pm_user where '.$sql.'';
                $connect = Yii::app()->db;
                $command = $connect->createCommand($sql);
                $a=$command->execute();
                if($a) return 1;
                else return 0;
            }
            else return 0;

        }

    }
?>